﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class panelDebtInfo : UserControl
    {
        private static EmployeeItem empItem;

        public panelDebtInfo()
        {
            InitializeComponent();
            this.grvDebtInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        public void setEmployee(EmployeeItem item)
        {
            empItem = item;
        }

        private void genGridview()
        {
            // gen Grideview
            grvDebtInfo.AutoGenerateColumns = false;

            //Set Columns Count
            grvDebtInfo.ColumnCount = 6;

            grvDebtInfo.Columns[0].Width = 45;

            //Add Columns
            grvDebtInfo.Columns[1].Name = "LOAN_ID";
            grvDebtInfo.Columns[1].HeaderText = "รหัสการขอกู้";
            grvDebtInfo.Columns[1].DataPropertyName = "LOAN_ID";
            grvDebtInfo.Columns[1].Width = 90;

            grvDebtInfo.Columns[2].Name = "CUSTOMER_NAME";
            grvDebtInfo.Columns[2].HeaderText = "ชื่อ";
            grvDebtInfo.Columns[2].DataPropertyName = "CUSTOMER_NAME";
            grvDebtInfo.Columns[2].Width = 130;

            grvDebtInfo.Columns[3].Name = "CUSTOMER_LASTNAME";
            grvDebtInfo.Columns[3].HeaderText = "นามสกุล";
            grvDebtInfo.Columns[3].DataPropertyName = "CUSTOMER_LASTNAME";
            grvDebtInfo.Columns[3].Width = 145;

            grvDebtInfo.Columns[4].Name = "UN_PAID_QTY";
            grvDebtInfo.Columns[4].HeaderText = "จำนวนงวดที่ค้างชำระ";
            grvDebtInfo.Columns[4].DataPropertyName = "UN_PAID_QTY";
            grvDebtInfo.Columns[4].Width = 130;

            grvDebtInfo.Columns[5].Name = "UN_PAID";
            grvDebtInfo.Columns[5].HeaderText = "ยอดค้างชำระ";
            grvDebtInfo.Columns[5].DataPropertyName = "UN_PAID";
            grvDebtInfo.Columns[5].Width = 100;
            // end gen gridview
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmDebtInfo frmDebtInfo = new frmDebtInfo(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()), ((DataGridView)sender).Rows[e.RowIndex].Cells[5].Value.ToString());
                frmDebtInfo.ShowDialog();
            }
        }

        private DataTable LoadDept()
        {
            DateTime date = DateTime.Now;
            string tmpDate = date.Year + "-" + date.Month + "-" + date.Day;

            DataTable dt = DAL.DeptInfoDAL.LoanAllDept(tmpDate, empItem.EMPLOYEE_ID, 
                this.txtLoanId.Text, this.txtName.Text, this.txtLastName.Text);

            return dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            grvDebtInfo.DataSource = LoadDept();
        }
    }
}
